﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Stamp_For_Africa_Version_2.Models;
using Stamp_For_Africa_Version_2.Repositories;

namespace Stamp_For_Africa_Version_2.Controllers
{

	public static class ControllerHelpers
	{

		public static void AddRuleViolations(this ModelStateDictionary modelState, IEnumerable<RuleViolation> errors)
		{

			foreach (RuleViolation issue in errors)
			{
				modelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
			}
		}
	}

	public class ArtistsController : Controller
	{
		readonly ArtistRepository _artistRepository = new ArtistRepository();

    private Guid GetUserId()
    {
      MembershipUser user = Membership.GetUser();
      Guid userId = Guid.Empty;
      if (user != null)
        userId = (Guid)user.ProviderUserKey;

      return userId;
    }

		#region HTTP - GET Methods

		//
		// GET: /Artists/ArtistsIndex

		public ActionResult Index()
		{

			var artists = _artistRepository.FindAllArtists().ToList().OrderBy(u => u.Surname);

			return View("Index", artists);
		}

		//
		// GET: /Artists/ArtistDetails/Id

		public ActionResult Details(Guid id)
		{

			Artist artist = _artistRepository.GetArtist(id);

			if (artist == null)
				return View("NotFound");
			else
				return View("Details", artist);
		}

		//
		// GET: /Artists/ArtistCreate

		public ActionResult Create()
		{

			Artist artist = new Artist()
			{
				
			};

			return View(artist);
		}

		//
		// GET: /Artists/ArtistEdit/2

		public ActionResult Edit(Guid id)
		{

			Artist artist = _artistRepository.GetArtist(id);

			return View(artist);
		}


		//
		// GET: /Artists/Delete/xxx

		public ActionResult Delete(Guid id)
		{

			Artist artist = _artistRepository.GetArtist(id);

			if (artist == null)
				return View("NotFound");
			else
				return View(artist);
		}

    [AcceptVerbs(HttpVerbs.Get)]
    public ActionResult ShowArtist(Guid id)
    {
      ArtistRepository repository = new ArtistRepository();

      Artist artist = repository.GetArtist(id);
      if (artist != null)
        return File(artist.Image.ToArray(), "image/jpg");
      return null;
    }

		#endregion

		#region HTTP - POST Methods

		//
		// POST: /Artists/Create

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Create( Artist artist)
		{
			if (ModelState.IsValid)
			{

				try
				{
					artist.Id = Guid.NewGuid();
					_artistRepository.Add(artist);
					_artistRepository.Save();


					return RedirectToAction("Details", new { id = artist.Id });
				}
				catch
				{
					ModelState.AddRuleViolations(artist.GetRuleViolations());
				}
			}

			return View(artist);
		}

		//
		// POST: /Artists/Edit/xxx

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Edit(Guid id, FormCollection formValues)
		{
			Artist artist = _artistRepository.GetArtist(id);

			try
			{
			
				UpdateModel(artist);
				_artistRepository.Save();

				return RedirectToAction("Details", new { id = artist.Id });
			}
			catch
			{
					ModelState.AddRuleViolations(artist.GetRuleViolations());

					return View(artist);
			}
			

		}

		//
		// HTTP POST: /Artists/Delete/xxx

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Delete(Guid id, string confirmButton)
		{

			Artist artist = _artistRepository.GetArtist(id);

			if (artist == null)
				return View("NotFound");

			_artistRepository.Delete(artist);
			_artistRepository.Save();

			return View("Deleted");
		}

		#endregion
	}
}
